<?php
class NewsAction extends GlobalAction{
	public function index(){
	$this->news_list();
	}
	public function news_list(){
    	$news = M('news');
    	import("@.ORG.Page"); //导入分页类 
        $count = $news->count();    //计算总数 
        $p = new Page ( $count, 20 ); 
    	$news = $news->limit($p->firstRow.','.$p->listRows)->order('news_id asc')->findall();
    	$page = $p->show (); 
        $this->assign( "page", $page ); 
    	$this->assign('news',$news);
    	$this->GlobalName();
		$this->display('list');
    }
	public function news_search(){
        $search['title'] = array('like','%'.$_POST['search'].'%');
        $search['e_title'] = array('like','%'.$_POST['search'].'%');
        $search['overview'] = array('like','%'.$_POST['search'].'%');
        $search['e_overview'] = array('like','%'.$_POST['search'].'%');
        $search['author'] = array('like','%'.$_POST['search'].'%');
        $search['fromr'] = array('like','%'.$_POST['search'].'%');
        $search['contents'] = array('like','%'.$_POST['search'].'%');
        $search['e_contents'] = array('like','%'.$_POST['search'].'%');
        $search['_logic'] = 'or';
    	$news = M('news');
    	import("@.ORG.Page"); //导入分页类 
        $count = $news->where($search)->count();    //计算总数 
        $p = new Page ( $count, 20 ); 
    	$news = $news->where($search)->limit($p->firstRow.','.$p->listRows)->order('news_id asc')->findall();
    	$page = $p->show (); 
        $this->assign( "page", $page ); 
    	$this->assign('news',$news);
    	$this->GlobalName();
		$this->display('list');
    }
    
    public function add(){
    	$sort = M('news');
    	$sort = $sort->distinct('distinct')->Field('sort,e_sort')->findall();
    	$this->assign('sort',$sort);
    	$this->GlobalName();
		$this->display();
    }
    public function news_news_get(){
    	$_POST['time'] = time();
    	$news = M('news');
    	if (!empty($_FILES['pic']['name'])) {
    		$_POST['pic'] = $this->_upload('0');
    	}
    	$news->create();
    	if ($news->add()) {
    		$this->get_alert('新增成功');
    	};
    }
    
    public function edit(){
    	$pid = $_GET['pid'];
    	$news = M('news');
    	$news = $news->where('news_id = '.$pid)->findall();
    	$this->assign('news',$news);
    	$sort = M('news');
    	$sort = $sort->distinct('distinct')->Field('sort,e_sort')->findall();
    	$this->assign('sort',$sort);
    	$this->GlobalName();
		$this->display();
    }
    public function news_edit_get(){
    	$news = M('news');
    	if (!empty($_FILES['pic']['name'])) {
    		$_POST['pic'] = $this->_upload('0');
    	}else{
    		$_POST['pic'] = $this->cut_pic_s_url($_POST['pic']);
    	}
    	$news->create();
    	if ($news->save()) {
    		$this->get_alert('修改成功');
    	};
    }
    
    public function news_delete(){
    	$news = M('news');
    	$del = $_POST['del'];
    	$delete = $del[0];
    	for ($i=1;$i < count($del);$i++){
    		$delete = $delete.','.$del[$i];
    	}
    	if ($news->delete($delete)) {
    		$this->get_alert('删除成功');
    	};
    }
    
    public function ajax_show(){
    	$this->ajax_get('news','show_m');
    }
    public function left(){//默认左列表
    	$left = array(
    		array('sort'=>'News','name'=>'新闻管理','value'=>'news_list')
    	);
     	$this->assign('user',$_SESSION['User']);
    	$this->assign('sort',$sort);
    	$this->assign('left',$left);
    	$this->display('Index:left');
    }
}
?>